# coding = utf-8

'''
图像和血管进行同步分析
'''

import cv2,os
import numpy as np
import matplotlib.pyplot as plt
import SimpleITK as sitk
from PIL import Image
import math

def find_data(case_id, origion_id, index):
    big_image = "E:\Dataset\Liver\qiye\DongBeiDaXue2\image_venous\\data2_{}_venous.mha".format(origion_id)
    big_liver = "E:\Dataset\Liver\qiye\DongBeiDaXue2\liver\\data2_{}_liver_label.mha".format(origion_id)
    big_tumor = "E:\Dataset\Liver\qiye\DongBeiDaXue2\lesion\\data2_{}_lesion_label.mha".format(origion_id)
    hepatic_vein = "E:\Dataset\Liver\qiye\DongBeiDaXue2\hepatic_vein\\data2_{}_hepatic_vein_label.mha".format(origion_id)
    portal_vein = "E:\Dataset\Liver\qiye\DongBeiDaXue2\portal_vein\\data2_{}_portal_vein_label.mha".format(origion_id)
    big_fusion = "E:\predict\image_tumor\case_{}\\fusion\\{}.png".format(str(case_id).zfill(5), str(index).zfill(3))
    big_image = sitk.GetArrayFromImage(sitk.ReadImage(big_image))
    big_image[big_image <= -200] = -200
    big_image[big_image > 250] = 250
    big_image = (big_image + 200) / 450
    big_image = big_image[index]
    big_liver = sitk.GetArrayFromImage(sitk.ReadImage(big_liver))
    big_liver = big_liver[index]
    big_tumor = sitk.GetArrayFromImage(sitk.ReadImage(big_tumor))
    big_tumor = big_tumor[index]
    big_fusion = Image.open(big_fusion)
    hepatic_vein = sitk.GetArrayFromImage(sitk.ReadImage(hepatic_vein))
    hepatic_vein = hepatic_vein[index]
    portal_vein = sitk.GetArrayFromImage(sitk.ReadImage(portal_vein))
    portal_vein = portal_vein[index]

    return (big_image, big_fusion, big_liver, big_tumor, hepatic_vein, portal_vein)



def get_data(case_id, origion_id, index):
    (big_image, big_fusion, big_liver, big_tumor, hepatic_vein, portal_vein) = find_data(case_id=case_id, origion_id=origion_id,
                                                                                         index=index)

    big_image = big_image * 255
    big_image = big_image.astype(np.uint8)
    hepatic_vein[hepatic_vein > 0] = 1
    hepatic_vein = hepatic_vein * 255
    hepatic_vein = hepatic_vein.astype(np.uint8)
    portal_vein[portal_vein > 0] = 1
    portal_vein = portal_vein * 255
    portal_vein = portal_vein.astype(np.uint8)
    big_liver[big_liver > 0] = 1
    big_liver = big_liver * 255
    big_liver = big_liver.astype(np.uint8)
    big_tumor[big_tumor > 0] = 1
    big_tumor = big_tumor * 255
    big_tumor = big_tumor.astype(np.uint8)

    image = cv2.cvtColor(big_image, cv2.COLOR_GRAY2BGR)
    contours, _ = cv2.findContours(big_liver, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for counter in contours:
        data_list = []
        for t in range(counter.shape[0]):
            j = counter[t][0]
            data_list.append(j)
        cv2.polylines(image, np.array([data_list], np.int32), True, [255, 0, 0], thickness=1)

    contours, _ = cv2.findContours(big_tumor, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for counter in contours:
        data_list = []
        for t in range(counter.shape[0]):
            j = counter[t][0]
            data_list.append(j)
        cv2.polylines(image, np.array([data_list], np.int32), True, [0, 255, 0], thickness=1)

    contours, _ = cv2.findContours(hepatic_vein, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for counter in contours:
        data_list = []
        for t in range(counter.shape[0]):
            j = counter[t][0]
            data_list.append(j)
        cv2.polylines(image, np.array([data_list], np.int32), True, [0, 0, 255], thickness=1)

    contours, _ = cv2.findContours(portal_vein, cv2.RETR_TREE, cv2.CHAIN_APPROX_SIMPLE)
    for counter in contours:
        data_list = []
        for t in range(counter.shape[0]):
            j = counter[t][0]
            data_list.append(j)
        cv2.polylines(image, np.array([data_list], np.int32), True, [0, 255, 255], thickness=1)

    return image

def show_data():
    image1 = get_data(case_id=79, origion_id="0865", index=249)
    image2 = get_data(case_id=79, origion_id="0865", index=250)
    image3 = get_data(case_id=79, origion_id="0865", index=251)

    plt.subplot(1, 3, 1)
    plt.imshow(image1)
    plt.subplot(1, 3, 2)
    plt.imshow(image2)
    plt.subplot(1, 3, 3)
    plt.imshow(image3)
    plt.show()








if __name__ == '__main__':
    show_data()